package com.example.liliangwei.topnewslearn;

import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.liliangwei.topnewslearn.adapter.NewsFragmentPagerAdapter;
import com.example.liliangwei.topnewslearn.bean.NewsClassify;
import com.example.liliangwei.topnewslearn.fragment.NewsFragment;
import com.example.liliangwei.topnewslearn.tool.BaseTools;
import com.example.liliangwei.topnewslearn.tool.Constants;
import com.example.liliangwei.topnewslearn.view.ColumnHorizontalScrollView;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.util.ArrayList;

import static android.support.v4.view.ViewPager.*;
import static android.view.View.*;

public class MainActivity extends FragmentActivity {

    private int mScreenWidth = 0;
    private int mItemWidth = 0;
    private ColumnHorizontalScrollView mColumnHorizontalScrollView;
    private LinearLayout mRadioGroup_content;
    private LinearLayout ll_more_columns;
    private RelativeLayout rl_column;
    private ImageView button_more_columns;
    private ViewPager mViewPager;
    private ImageView shade_left;
    private ImageView shade_right;
    private ImageView top_head;
    private ImageView top_more;
    private ImageView top_refresh;
    private ProgressBar top_progress;
    protected SlidingMenu side_drawer;
    private ArrayList<NewsClassify> newsClassify = new ArrayList<NewsClassify>();
    private int columnSelectIndex = 0;
    private ArrayList<Fragment> fragments = new ArrayList<Fragment>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mScreenWidth = BaseTools.getWindowWidth(this);
        mItemWidth = mScreenWidth / 7;
        
       initView();
    }

    private void initView() {
        mColumnHorizontalScrollView = (ColumnHorizontalScrollView)findViewById(R.id.mColumnHorizontalScrollView);
        mRadioGroup_content = (LinearLayout) findViewById(R.id.mRadioGroup_content);
        ll_more_columns = (LinearLayout) findViewById(R.id.ll_more_columns);
        rl_column = (RelativeLayout) findViewById(R.id.rl_column);
        button_more_columns = (ImageView) findViewById(R.id.button_more_columns);
        mViewPager = (ViewPager) findViewById(R.id.mViewPager);
        shade_left = (ImageView) findViewById(R.id.shade_left);
        shade_right = (ImageView) findViewById(R.id.shade_right);
        top_head = (ImageView) findViewById(R.id.top_head);
        top_more = (ImageView) findViewById(R.id.top_more);
        top_refresh = (ImageView) findViewById(R.id.top_refresh);
        top_progress = (ProgressBar) findViewById(R.id.top_progress);
        button_more_columns.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        top_head.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (side_drawer.isSecondaryMenuShowing()){
                    side_drawer.showContent();
                }else {
                    side_drawer.showSecondaryMenu();
                }
            }
        });
        setChangelView();

    }

    private void setChangelView() {
        initColumnData();
        initTabClolum();
        initFragment();
    }

    private void initFragment() {
        int count = newsClassify.size();
        for (int i = 0; i < count; i++){
            Bundle data = new Bundle();
            data.putString("text", newsClassify.get(i).getNewsTitle());
            NewsFragment newsFragment = new NewsFragment();
            newsFragment.setArguments(data);
            fragments.add(newsFragment);
        }
        NewsFragmentPagerAdapter mAdapter = new NewsFragmentPagerAdapter(getSupportFragmentManager(),fragments);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOnPageChangeListener(pageListener);
    }

    public OnPageChangeListener pageListener = new OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            
        }

        @Override
        public void onPageSelected(int position) {
            mViewPager.setCurrentItem(position);
            selectTab(position);//screen roll function
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    private void selectTab(int position) {
        columnSelectIndex = position;
        for (int i=0; i < mRadioGroup_content.getChildCount();i++){
            View checkView = mRadioGroup_content.getChildAt(position);
            int k = checkView.getMeasuredWidth();
            int l = checkView.getLeft();
            int i2 = l + k/2 - mScreenWidth / 2;
            mColumnHorizontalScrollView.smoothScrollTo(i2,0);
        }

        for (int j = 0; j < mRadioGroup_content.getChildCount();j++){
            View checkView = mRadioGroup_content.getChildAt(j);
            boolean isCheck;
            if (j == position){
                isCheck = true;
            }else {
                isCheck = false;
            }
            checkView.setSelected(isCheck);
        }
    }

    private void initTabClolum() {
        mRadioGroup_content.removeAllViews();
        int count = newsClassify.size();
        mColumnHorizontalScrollView.setParam(this,mScreenWidth,mRadioGroup_content,shade_left,shade_right,ll_more_columns,rl_column);
        for(int i = 0; i< count; i++){
            LayoutParams params = new LayoutParams(mItemWidth, LayoutParams.WRAP_CONTENT);

            TextView columnTextView = new TextView(this);
            columnTextView.setTextAppearance(this, R.style.top_category_scroll_view_item_text);
            columnTextView.setBackgroundResource(R.drawable.radio_buttong_bg);
            columnTextView.setGravity(Gravity.CENTER);
            columnTextView.setPadding(15, 15, 15, 15);
            columnTextView.setId(i);
            columnTextView.setText(newsClassify.get(i).getNewsTitle());
            columnTextView.setTextColor(getResources().getColorStateList(R.color.top_category_scroll_text_color_day));
            if (columnSelectIndex == i){
                columnTextView.setSelected(true);
            }
            columnTextView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    for (int i = 0; i < mRadioGroup_content.getChildCount();i++){
                        View localView = mRadioGroup_content.getChildAt(i);
                        if (localView != view){
                            localView.setSelected(false);
                        }else {
                            localView.setSelected(true);
                            mViewPager.setCurrentItem(i);
                        }
                    }
                    Toast.makeText(getApplicationContext(),newsClassify.get(view.getId()).getNewsTitle(), Toast.LENGTH_SHORT).show();
                }
            });
            mRadioGroup_content.addView(columnTextView,i,params);
        }
    }

    private void initColumnData() {
        newsClassify = Constants.getData();
    }


}
